package com.sunyur.tailor.channel.service.inner.matrix.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelListResult;
import com.sunyur.tailor.channel.common.middle.util.ChannelListResultUtil;
import com.sunyur.tailor.channel.service.inner.matrix.model.GetMatrixRuleModel;
import com.sunyur.tailor.channel.service.inner.matrix.param.GetMatrixRuleParam;
import com.sunyur.tailor.channel.service.inner.matrix.service.MatrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: HuBaoding
 * @Description:
 * @Date: Created in 15:24 2021/8/23
 */
@Slf4j
@Service("matrixService")
public class MatrixServiceImpl implements MatrixService {

    @Autowired
    private SunyurClientUtil sunyurClientUtil;

    @Value("${sunyur.api.getMatrixRules}")
    private String getMatrixRules;

    @Override
    public ChannelListResult<GetMatrixRuleModel> getRules(GetMatrixRuleParam getMatrixRuleParam) {
        log.info("MatrixServiceImpl getRules param is {}", JSON.toJSONString(getMatrixRuleParam));

        SunyurResult sunyurResult = sunyurClientUtil.execPost(getMatrixRules, JSON.toJSONString(getMatrixRuleParam), new TypeReference<SunyurResult<List<GetMatrixRuleModel>>>() {
        });
        log.info("MatrixServiceImpl getRules result is {}",JSON.toJSONString(sunyurResult));
        // 扩展坞报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelListResultUtil.error(sunyurResult.getCode(), sunyurResult.getErrorMessage());
        }
        List<GetMatrixRuleModel> getMatrixRuleModels = JSON.parseArray(JSON.toJSONString(sunyurResult.getContent()), GetMatrixRuleModel.class);
        return ChannelListResultUtil.success(getMatrixRuleModels);
    }
}
